
#ifndef SIMPLE_TCP_SERVER_H
#define SIMPLE_TCP_SERVER_H

#include "fiber-env.h"

class ConnectionHandler
{
public:
    virtual ~ConnectionHandler() {}

public:
    virtual void HandleConnection(fasmio::fiber_env::ITCPSocket*, const char* addr) = 0;
};

class SimpleTCPServer
{
public:
    SimpleTCPServer();
    virtual ~SimpleTCPServer();

public:
    bool Start(unsigned short port, ConnectionHandler* (*conn_creator)());
    void Stop();

protected:
    void log(const char* format, ...);

private:
    static int listen_fiber(void*);
    int listen_fiber();

    static int connection_fiber(void*);
    int connection_fiber(fasmio::fiber_env::ITCPSocket*, const char* addr);

private:
    fasmio::fiber_env::FiberEnv *fiber_env_;
    bool quit_flag_;
    unsigned short port_;
    ConnectionHandler* (*conn_creator_)();
};

#endif  // SIMPLE_TCP_SERVER_H

